<!DOCTYPE html>
<!--suppress JSUnresolvedVariable -->
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>midi转txt</title>
    <!--<script src="https://unpkg.com/@tonejs/midi"></script>-->
    <script src="js/Midi.js"></script>
</head>
<body style="size: 2em">
请选择文件：
<input id="f" type="file" onchange="midi2lua()" accept=".mid"><br>
注：选择文件后显示在下方的内容已自动复制，直接去新建文件粘贴就可。
<br>音阶：<input id="j" type="text" value="4" onchange="midi2lua()">
<div id="txt"></div>
</body>
<script>
    key ={'5': {
        'A#5': 5,
        'A#6': 12,
        'A5': 5,
        'A6': 12,
        'B5': 6,
        'B6': 13,
        'H#': 13,
        'H':13,
        'C#4': 0,
        'C#5': 7,
        'C4': 0,
        'C5': 7,
        'C6': 14,
        'C#6': 14,
        'D#4': 1,
        'D#5': 8,
        'D4': 1,
        'D5': 8,
        'E4': 2,
        'E5': 9,
        'F#4': 3,
        'F#5': 10,
        'F4': 3,
        'F5': 10,
        'G#4': 4,
        'G#5': 11,
        'G4': 4,
        'G5': 11
    },'4': {
        'A#4': 5,
        'A#5': 12,
        'A4': 5,
        'A5': 12,
        'B4': 6,
        'B5': 13,
        'H#': 13,
        'H':13,
        'C#4': 0,
        'C#5': 7,
        'C4': 0,
        'C5': 7,
        'C6': 14,
        'C#6': 14,
        'D#4': 1,
        'D#5': 8,
        'D4': 1,
        'D5': 8,
        'E4': 2,
        'E5': 9,
        'F#4': 3,
        'F#5': 10,
        'F4': 3,
        'F5': 10,
        'G#4': 4,
        'G#5': 11,
        'G4': 4,
        'G5': 11
    },'3':{
        'A#3': 5,
        'A#4': 12,
        'A3': 5,
        'A4': 12,
        'B3': 6,
        'B4': 13,
//        'H#': 13,
//        'H':13,
        'C#3': 0,
        'C#4': 7,
        'C3': 0,
        'C4': 7,
        'C5': 14,
        'C#5': 14,
        'D#3': 1,
        'D#4': 8,
        'D3': 1,
        'D4': 8,
        'E3': 2,
        'E4': 9,
        'F#3': 3,
        'F#4': 10,
        'F3': 3,
        'F4': 10,
        'G#3': 4,
        'G#4': 11,
        'G3': 4,
        'G4': 11
    }
    }
    function copyToClip(content, message) {
        var aux = document.createElement("input");
        aux.setAttribute("value", content);
        document.body.appendChild(aux);
        aux.select();
        document.execCommand("copy");
        document.body.removeChild(aux);
        if (message ) {
            alert(message);
        }
    }
    function midi2lua() {
//    var objFSO = new ActiveXObject("Scripting.FileSystemObject");
//        let f=document.getElementsByTagName("input")[0]
//        strSourFile=f.value
//        strDestFile='H:/yinyingyang/mid/'+f.files[0].name
//        var strPath = objFSO.MoveFile(strSourFile, strDestFile);
//        var reader = new FileReader()
//        bytes=reader.readBytes(f.files[0])
        //    const midi = new Midi()
        // load a midi file in the browser
//        Midi.fromUrl()
        const midi = Midi.fromUrl('mid/'+f.files[0].name)
        //the file name decoded from the first track
//        const name = midi.name
        //get the tracks
        console.log(midi.name)
        t = [{name:midi.name,songNotes: []}]
        g=j.value||4
        var rigth=0,err=0
        midi.then((resolve, reject)=>  {
//        time=0
            t[0].bpm = resolve.header.ppq * 4
            resolve.tracks.forEach(track=>  {
                //tracks have notes and controlChanges
                //notes are an array
                const notes = track.notes
                notes.forEach(note=> {
//                    console.log(note.duration)
                    if (note.duration>=0.1) {
                        ++rigth;
                        if (key[g][note.name] != undefined) {
                            //note.midi, note.time, note.duration, note.name
                            let s = {}
                            //                s[0]=parseInt((note.time-time)/sTime)
                            s.time = parseInt(note.time * 1000)
                            s.key = '1Key' + key[g][note.name]
                            t[0].songNotes.push(s)
                        } else {
                            ++err;
                            console.log(note.name)
                        }
                    }
                })

                //the control changes are an object
                //the keys are the CC number
//            track.controlChanges[64]
//            //they are also aliased to the CC number's common name (if it has one)
//            track.controlChanges.sustain.forEach(cc => {
//                // cc.ticks, cc.value, cc.time
//            })

                //the track also has a channel and instrument
                //track.instrument.name
            })
        })//.then(
//            t[0].songNotes.sort((a, b)=>{
//                return a.time < b.time
//            })
//        )
//    t.forEach(n=>{
//
//        console.log(n.name)
//        u[n.name]=1;
//    })
        setTimeout(()=> {
            if(rigth/err>1.5&&confirm('可能选错音阶，是否不修改继续运行？')) {
                let str = JSON.stringify(t)
                console.log(str)
                copyToClip(str, '复制成功，直接去新建文件粘贴就可')
                txt.innerText = str
            }
        }, 1000)
    }
</script>

</html>